package com.ecgsystem.activity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.concurrent.ConcurrentLinkedQueue;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.ecgsystem.R;
import com.ecgsystem.constants.Commons;
import com.ecgsystem.constants.Constants;
import com.ecgsystem.listadapter.RecordListAdapter;

/**
 * TODO Put here a description of what this class does.
 * 
 * @author ANHTUAN. Created Sep 28, 2012.
 */
public class PatientInfo extends Activity implements Constants{
	/**
	 * TODO Put here a description of this field.
	 */
	public static Context contextPI;
	private String patientName;
	private String patientID;
	private String birthday;
	private String gender;

	/**
	 * TODO Put here a description of this field.
	 */
	public static boolean STARTDRAW = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.patient_info);
		contextPI = this;

		Intent i = this.getIntent();
		int pos = i.getIntExtra("clickedposition", 0);

		HashMap<String, String> patient = PatientList.patientListArr.get(pos);
		this.patientName = patient.get(TAG_PATIENT_NAME);
		this.patientID = patient.get(TAG_PATIENT_ID);
		Commons.PATIENTID = this.patientID;
		this.gender = patient.get(TAG_GENDER);
		this.birthday = patient.get(TAG_PATIENT_AGE);

		TextView patientNameTextView = (TextView) findViewById(R.id.textView_name_info);
		patientNameTextView.setText(this.patientName);

		TextView patientIDTextView = (TextView) findViewById(R.id.textView_id_info);
		patientIDTextView.setText(this.patientID);

		TextView patientBirthdayTextView = (TextView) findViewById(R.id.textView_birthday_info);
		patientBirthdayTextView.setText(this.birthday.substring(0, 10));

		TextView patientAgeTextView = (TextView) findViewById(R.id.textView_age_info);
		String ages = getAge(Integer.valueOf(birthday.substring(0, 4)),
				Integer.valueOf(birthday.substring(5, 7)),
				Integer.valueOf(birthday.substring(8, 10)));
		patientAgeTextView.setText(ages);

		TextView patientSexTextView = (TextView) findViewById(R.id.textView_gender_info);
		if (this.gender.equals("true")) {
			patientSexTextView.setText("Nam");
		} else {
			patientSexTextView.setText("Nữ");
		}

		ListView recordListView = (ListView) findViewById(R.id.listView_his_rec);

		final ArrayList<HashMap<String, String>> recordListArr;
		recordListArr = MainActivity.conn.getRecordHistoryList(this.patientID);

		if (recordListArr != null) {
			RecordListAdapter adapter = new RecordListAdapter(this,
					recordListArr);
			recordListView.setAdapter(adapter);
		}
		SetListViewHeightBasedOnChildren.ModifyHeight(recordListView);
		recordListView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				OfflineECG.QueueDraw = (ConcurrentLinkedQueue<Float>[]) new ConcurrentLinkedQueue[12];
				for (int i = 0; i < 12; i++) {
					OfflineECG.QueueDraw[i] = new ConcurrentLinkedQueue<Float>();
				}
				String recordID = recordListArr.get(arg2).get(TAG_RECORD_ID);

				Intent i = new Intent(PatientInfo.this, OfflineECG.class);
				i.putExtra(TAG_PATIENT_NAME, patientName);
				i.putExtra(TAG_UNUSUALSIGNAL_ID, "0");
				i.putExtra(TAG_PATIENT_ID, patientID);
				i.putExtra(TAG_RECORD_ID, recordID);
				i.putExtra(TAG_RECORD_DATE, recordListArr.get(arg2).get(TAG_RECORD_DATE));

				startActivity(i);
			}
		});

	}

	@Override
	public boolean onKeyDown(final int keyCode, final KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
		}
		return false;
	}
	
	public static class SetListViewHeightBasedOnChildren {

		 public static void ModifyHeight(ListView listView) {
			 RecordListAdapter listAdapter = (RecordListAdapter) listView.getAdapter();
		  if (listAdapter == null) {
		   return;
		  }
		  int totalHeight = 0;
		  for (int i = 0; i < listAdapter.getCount(); i++) {
		   View listItem = listAdapter.getView(i, null, listView);
		   listItem.measure(0, 0);
		   totalHeight += listItem.getMeasuredHeight();
		  }

		  ViewGroup.LayoutParams params = listView.getLayoutParams();
		  params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		  listView.setLayoutParams(params);
		  listView.requestLayout();
		 }
		}
	
	public static String getAge(int year, int month, int day) {
		Calendar dob = Calendar.getInstance();
		Calendar today = Calendar.getInstance();

		dob.set(year, month, day);
		int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);

		if (today.get(Calendar.DAY_OF_YEAR) < dob.get(Calendar.DAY_OF_YEAR)) {
			age--;
		}
		Integer ageInt = new Integer(age);
		String ageS = ageInt.toString();

		return ageS;
	}
}